package com.tedu._07sptingaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Objects;

/*
* 日志功能切面类
* 标识该类为切面类
* */
@Aspect
@Component
public class LogAspect {
    @Pointcut(value ="execution(public int com.tedu._07sptingaop.aop.CalculatorImpl.*(..))")
    public void dotime(){
    }
    //@Before(value= "dotime()")
    public void beforeMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("前置通知,"+name+"方法开始前插入参数为"+args);
    }
    //@After(value ="dotime()")
    public void afterMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("后置通知"+name+"方法结束");
    }
    //@AfterReturning(value= "dotime()",returning = "result")
    public void afterMethodReturning(JoinPoint joinPoint, Object result){
        String name = joinPoint.getSignature().getName();
        System.out.println("返回通知"+"返回值"+result+"方法"+name+"执行结束");
    }
    //@AfterThrowing(value = "dotime()",throwing = "ex")
    public void afterException(JoinPoint joinPoint,Throwable ex){
        String name = joinPoint.getSignature().getName();
        String message = ex.getMessage();
        System.out.println("异常通知"+name+"方法抛出异常异常消息为"+message);
    }
    //环绕通知,相当与前四种功能的综合
    @Around(value = "dotime()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        Object result = null;
        try {
            System.out.println("1.前置通知");
            result = joinPoint.proceed();
            System.out.println("3.返回通知");

        } catch (Throwable e) {
            System.out.println("4异常通知");
        } finally {
            System.out.println("5后置通知");
        }
        return result;
    }

}














